﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using Ext.Net;
using System.Collections.Generic;

public partial class Popup_Popup_Mat_Add : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            this.hdRegNo.Text = Request.QueryString["regno"];
            SetCombo();
        }
    }

    private void SetCombo()
    {
        using (EmdisBest.WebService.Service oService = new EmdisBest.WebService.Service())
        {
            DataSet ds = oService.Select_SP_DDDW_MCLASS_LEVEL1_S();
            StrClass1.DataSource = ds.Tables[0];
            StrClass1.DataBind();

            this.cbxUpClass.SelectedIndex = 0;

            ds = oService.Select_SP_DDDW_MCLASS_LEVEL2_S("");
            StrClass2.DataSource = ds.Tables[0];
            StrClass2.DataBind();

            this.cbxClass.SelectedIndex = 0;
        }
    }

    protected void ChangeGubun(object sender, DirectEventArgs e)
    {
        using (EmdisBest.WebService.Service oService = new EmdisBest.WebService.Service())
        {
            StrClass2.RemoveAll();
            
            DataSet ds = oService.Select_SP_DDDW_MCLASS_LEVEL2_S(this.cbxUpClass.SelectedItem.Value);
            StrClass2.DataSource = ds.Tables[0];
            StrClass2.DataBind();

            this.cbxClass.SelectedIndex = 0;
        }
    }

    protected void MovePara(object sender, DirectEventArgs e)
    {
        string json = e.ExtraParams["Values"];

        Dictionary<string, string>[] dicValues = JSON.Deserialize<Dictionary<string, string>[]>(json);
        string strVal = string.Empty;

        foreach (Dictionary<string, string> dic in dicValues)
        {
            foreach (KeyValuePair<string, string> row in dic)
            {
                strVal += row.Value + "|";
            }
        }

        strVal = strVal.Remove(strVal.Length - 1);
        this.tbxCode.Text = strVal.Split('|')[0];
        this.tbxName.Text = strVal.Split('|')[1];
        this.tbxSpec.Text = strVal.Split('|')[2];
        this.tbxUnit.Text = strVal.Split('|')[3];
        this.tbxUc.Text = strVal.Split('|')[4];
        this.tbxRemark.Text = strVal.Split('|')[5];
    }

    protected void Store_RefreshData(object sender, StoreRefreshDataEventArgs e)
    {
        try
        {
            using (EmdisBest.WebService.Service oService = new EmdisBest.WebService.Service())
            {
                Store1.RemoveAll();
                DataSet ds = oService.Select_D_MA_DB_S("", this.cbxClass.SelectedItem.Value.Equals("M%") ? "" : this.cbxClass.SelectedItem.Value, this.tbxMatName.Text, "", "");
                Store1.DataSource = ds.Tables[0];
                Store1.DataBind();
            }
        }
        catch (Exception ex)
        {
            throw ex;
        }
    }

    #region == Store_BeforeChanged(Store1 데이터가 변경되기전, 즉 Store에 들어오는 변경데이터를 파라미터로 보내옴) ==
    /// <summary>
    /// 실제 저장하는 이벤트
    /// Store1_BeforeChanged(Store1 데이터가 변경되기전, 즉 Store에 들어오는 변경데이터를 파라미터로 보내옴)
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    protected void Store_BeforeChanged(object sender, BeforeStoreChangedEventArgs e)
    {
        int nResult = 0;
        DataSet dsSave = null;
        try
        {
        }
        catch (Exception ex)
        {
            this.ResourceManager1.AddScript("setTimeout(function () { Ext.MessageBox.hide(); Ext.Msg.notify('알림', '저장에 실패했습니다. 잠시후 다시 시도해주십시오.'); }, 500);");
            throw ex;
        }
    }

    #endregion


    protected void btnSave_Click(object sender, DirectEventArgs e)
    {
        SaveMat300();
    }


    protected void SaveMat300()
    {
        try
        {
            using (EmdisBest.WebService.Service oService = new EmdisBest.WebService.Service())
            {
                int iResult;
                if (!string.IsNullOrEmpty(this.hdRegNo.Text))
                {
                    iResult = oService.INSERT_DW_FPD300T_I(this.tbxCode.Text, this.tbxName.Text, this.tbxSpec.Text, this.tbxUnit.Text, this.hdRegNo.Text, this.tbxUc.Text, this.tbxRemark.Text);
                }
            }

        }
        catch (Exception ex)
        {
            ex.ToString();
        }
    }


    protected void btnInser_Click(object sender, DirectEventArgs e)
    {
        var win = new Window
        {
            ID = "Window3",
            //Title = "My Window",
            Height = 200,
            Width = 650,
            Padding = 5
        };


        Ext.Net.Button btn = (Ext.Net.Button)sender;
        string id = btn.ID;

        switch (id)
        {
            case "btnMatInsert":
                win.Listeners.Hide.Handler = "btnMatInsert.setDisabled(false);";
                win.AutoLoad.Url = "Popup_Mat_Add_Add.aspx";
                win.Title = "자재 마스터 추가/수정";
                win.AutoLoad.Mode = LoadMode.IFrame;
                win.Render(this.Form);
                break;

            case "btnMatUpdate":
                win.Listeners.Hide.Handler = "btnMatUpdate.setDisabled(false);";
                win.AutoLoad.Url = "Popup_Mat_Add_Add.aspx?code=" + this.tbxCode.Text;
                win.Title = "자재 마스터 추가/수정";
                win.AutoLoad.Mode = LoadMode.IFrame;
                win.Render(this.Form);
                break;
            default:
                break;
        }
    }
}
